package com.freshcity.page.home
{
	import com.freshcity.ILayer;
	import com.freshcity.component.alert.Alert;
	import com.freshcity.component.container.Window;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.page.friend.RejectRequest;
	import com.freshcity.utils.GlobalUtil;
	import com.freshcity.utils.MessageUtil;
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	
	import fl.containers.ScrollPane;
	import fl.transitions.Tween;
	
	import flash.events.Event;
	import flash.events.TextEvent;
	import flash.geom.Point;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class UserLog extends Window
	{
		private var css:String="".concat('.my_status{color: #C5C5C5;letter-spacing:4px;}', "a{color: #0000ff;}", "a:hover{color:#ff0000;textDecoration: underline;}", ".c_tx4{color: #FE6600;}", "div{display: block;}", "span{}", "");

		private var _logData:String;
		private var sp:ScrollPane;
		private var logTf:TextField;

		public function UserLog(xpos:Number=0, ypos:Number=0, title:String="最新动态")
		{
			this.layer=LayerManager.sceneLayer;
			super(this.layer, xpos, ypos, title);
			this.mouseEnabled=false;
			this.hasCloseButton=true;
			this.setSize(500, 375);
		}

		override protected function addChildren():void
		{
			super.addChildren();
			sp=new ScrollPane();
			sp.move(10, 10);
			sp.setSize(480, 310);
			sp.focusEnabled=false;
			this.addChild(sp);

			logTf=new TextField();
			logTf.x=40;
			logTf.y=20;
			logTf.autoSize=TextFieldAutoSize.LEFT;
			logTf.wordWrap=true;
			logTf.multiline=true;
			logTf.selectable=true;
			logTf.width=460;
			logTf.htmlText="暂时没有任何动态信息";

			var format:TextFormat=new TextFormat("宋体", 13);
			format.leading=8;
			//format.kerning=true;
			format.letterSpacing=1;
			logTf.defaultTextFormat=format;

			var style:StyleSheet=new StyleSheet();
			style.parseCSS(css);
			logTf.styleSheet=style;
			sp.source=logTf;
		}

		override public function destroy():void
		{
			logTf.removeEventListener(TextEvent.LINK, onLinkHandler);
			super.destroy();
		}

		override public function hide(event:Event=null):void
		{
			var xv:Number=540;
			var vy:Number=30;
			GlobalUtil.regPoint(this, new Point(this.width / 2, this.height / 2));
			var timeLine:TimelineLite=new TimelineLite({onComplete: destroy});
			timeLine.append(new TweenLite(this, 0.5, {scaleX: 0.04, scaleY: 0.04}));
			timeLine.append(new TweenLite(this, 1, {x: xv, y: vy, delay: 0.1}));
			timeLine.append(new TweenMax(this, 0.2, {autoAlpha: 0, delay: 0.1}));
			//TweenLite.to(this, 1, {alpha:0, x:xv, y:vy, scaleX:0, scaleY:0,onComplete:onTweenComplete});	
			//TweenLite.to(this, 0.3, {alpha: 0, onComplete: destroy});
		}

		override public function show(event:Event=null):void
		{
			logTf.addEventListener(TextEvent.LINK, onLinkHandler);
			this.layer.setTop(this);
			this.x=(stage.stageWidth - this.width) / 2;
			this.y=(stage.stageHeight - this.height) / 2;
			TweenMax.from(this, 0.3, {autoAlpha: 0, y: "-50"});
		}

		private function onLinkHandler(event:TextEvent):void
		{
			var actionDesc:String=event.text;
			var locArray:Array=actionDesc.split('*');
			var loc1:String=locArray[0];
			var loc2:String=locArray[1];
			var loc3:String;

			switch (loc1)
			{
				case "u":
				{
					var evt:TextEvent=new TextEvent(TextEvent.LINK, false, false, loc2);
					main.onAboutUser(evt);
					break;
				}
				case "s":
				{
					MainScene.writeMsg.uid=loc2;
					MainScene.writeMsg.isReply=true;
					MainScene.writeMsg.onShow();
					break;
				}
				case "up":
				{
					main.statusPanel.showUpgrade();
					break;
				}
				case "nt":
				{
					this.visible = false;
					main.mainNav.onShowTree();
					break;
				}					
				case "thank":
				{
					main.commu.send(188, {to: loc2}, onThankWaterCallBack);
					break;
				}					
				case "fthank":
				{
					var req:RejectRequest = new RejectRequest(loc2);
					req.main = main;
					req.show();
					break;
				}					
				case "facp":
				{
					loc3 = locArray[2];
					acceptFriend(loc2,int(loc3));
					break;
				}					
				default:
				{
					break;
				}
			}
		}
		
		private function acceptFriend(uid:String,level:int):void
		{
			var htmlContent:String="确认要提升关系到"+ MessageUtil.decodeAssoLevel(level) +"吗？";
			var htmlTitle:String="";
			var myfun:Function = function(arg1:int):void{
				if (arg1 == Alert.OK)
				{
					main.commu.send(2309, {to: uid,l: level}, acceptFriendCallback);
				}
			}
			Alert.show(htmlContent, htmlTitle, Alert.OK | Alert.CANCEL, null, myfun);
		}
		
		private function acceptFriendCallback(data:*):void
		{
			if(data==true){
				main.msg.showRollTipsMsg("成功完成操作");
			}
		}		
		
		private function onThankWaterCallBack(data:*):void
		{
			if(data==true){
				main.msg.showRollTipsMsg("信息已发出");
			}
		}
		
		public function get logData():String
		{
			return _logData;
		}

		public function set logData(value:String):void
		{
			_logData=value;
			logTf.htmlText=_logData;
			sp.update();
		}
	}
}